//
//  ZJNewFeatureController.m
//  GuidePageDemo
//
//  Created by zhengju on 16/11/7.
//  Copyright © 2016年 俱哥. All rights reserved.
//

#import "ZJNewFeatureController.h"

#import "ZJNewFeatureCell.h"

#import "ViewController.h"

#import "HLogin2ViewController.h"

@interface ZJNewFeatureController ()<ZJNewFeatureCellDelegate>

@property (nonatomic, weak) UIPageControl *control;

@end

@implementation ZJNewFeatureController

static NSString * const reuseIdentifier = @"Cell";

- (instancetype)init
{
    
    
    UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc] init];
    
    
    // 设置cell的尺寸
    layout.itemSize = [UIScreen mainScreen].bounds.size;
    // 清空行距
    layout.minimumLineSpacing = 0;
    
    // 设置滚动的方向
    layout.scrollDirection = UICollectionViewScrollDirectionHorizontal;
    
    return [super initWithCollectionViewLayout:layout];
    
}

- (void)viewDidLoad {
    [super viewDidLoad];
    
    
    // 注册cell,默认就会创建这个类型的cell
    [self.collectionView registerClass:[ZJNewFeatureCell class] forCellWithReuseIdentifier:reuseIdentifier];
    
    // 分页
    self.collectionView.pagingEnabled = YES;
    //self.collectionView.bounces = NO;
    self.collectionView.showsHorizontalScrollIndicator = NO;
    
    
    // 添加pageController
     [self setUpPageControl];
}
// 添加pageController
- (void)setUpPageControl
{
    // 添加pageController,只需要设置位置，不需要管理尺寸
    UIPageControl *control = [[UIPageControl alloc] init];
    
    control.numberOfPages = 3;
    control.pageIndicatorTintColor = RGB(212, 212, 212);
    control.currentPageIndicatorTintColor = kNavBlueColor;
    
    // 设置center
    control.center = CGPointMake(ScreenWidth * 0.5, ScreenHeight - 10);
    _control = control;
    [self.view addSubview:control];
}

#pragma mark - UIScrollView代理
// 只要一滚动就会调用
- (void)scrollViewDidScroll:(UIScrollView *)scrollView
{
    // 获取当前的偏移量，计算当前第几页
    int page = scrollView.contentOffset.x / scrollView.bounds.size.width + 0.5;
    
    // 设置页数
    _control.currentPage = page;
    
    if (scrollView.contentOffset.x > (4 - 1)*ScreenWidth) {
        [self startClick];
    }
}
#pragma mark - UICollectionView代理和数据源
// 返回有多少组
- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView
{
    return 1;
}

// 返回第section组有多少个cell
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    return 3;
}



// 返回cell长什么样子
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{

    ZJNewFeatureCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:reuseIdentifier forIndexPath:indexPath];
    
    NSString *imageName;
    
        if (IPHONE5 || IPHONE4) {
            
            imageName = [NSString stringWithFormat:@"641x1136_0%ld",indexPath.row + 1];
            
        }else if (IPHONE6){
            
            imageName = [NSString stringWithFormat:@"751x1334_0%ld",indexPath.row +1];
            
        }else if (IPHONE6P){
            
            imageName = [NSString stringWithFormat:@"1080x1920_0%ld",indexPath.row +1 ];
            
        }else {
            
            imageName = [NSString stringWithFormat:@"1536x2048_0%ld",indexPath.row + 1];
        }
    
    cell.image = [UIImage imageNamed:imageName];
    
    [cell setIndexPath:indexPath count:4];
    cell.delegate = self;
    return cell;
    
}
// 点击开始的时候调用
- (void)startClick
{
    //切换根视图
    self.view.window.rootViewController          = [HLogin2ViewController new];
    
    //记录是否已经走过新特性
    [[NSUserDefaults standardUserDefaults]setBool:YES forKey:[NSString stringWithFormat:@"ISNEWFEATURE%@",App_Version]];
    
    [[NSUserDefaults standardUserDefaults]synchronize];
    
}
@end
